﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class CustomerPOController:ControllerBase
    {

        public int addCustomerPO(CustomerPOEntity customerPoEntity)
        {
           CustPO custPo= TransformCustomerEntityToCustPO(customerPoEntity);
           Sdc.CustPOs.InsertOnSubmit(custPo);
           Sdc.SubmitChanges();
           int custPOId = custPo.Id;
            return custPOId;
        }

        public static decimal CountTotalAmount(CustPO custPo)
        {
            IList<CustPOItem> custPoItems = custPo.CustPOItems.ToList();
            decimal totalAmount=0;
            for (int i = 0; i < custPoItems.Count; i++)
            {
                totalAmount = totalAmount + (custPoItems.ElementAt(i).Price * custPoItems.ElementAt(i).RequestQty);
            }
            return totalAmount;
        }

        public IList<CustomerPOEntity> FindPendingCustomerPOByCustomerId(int customerId)
        {
            IList<CustPO> customerPo = (from c in Sdc.CustPOs
                                        where c.CustId == customerId && c.Status!="completed"
                                        orderby c.No ascending
                                        select c).ToList();
            IList<CustomerPOEntity> customerPoEntities = new List<CustomerPOEntity>();
            foreach (var c in customerPo)
            {
                CustomerPOEntity customerPoEntity = TransformCustPOToCustomerPOEntity(c);
                customerPoEntities.Add(customerPoEntity);
            }
            return customerPoEntities;
        }

        public IList<CustomerPOEntity> findCustomerPOByStatus(string status)
        {
            IList<CustPO> customerPo = (from c in Sdc.CustPOs
                                                          where c.Status==status
                                        orderby c.Date descending, c.Id descending 
                                                          select c).ToList();
            IList<CustomerPOEntity> customerPoEntities=new List<CustomerPOEntity>();
            foreach (var c in customerPo)
            {
                CustomerPOEntity customerPoEntity = TransformCustPOToCustomerPOEntity(c);
                customerPoEntities.Add(customerPoEntity);
            }
            return customerPoEntities;
        }
        public CustomerPOEntity findCustomerPOById(int customerPOId)
        {
            CustPO customerPo = (from c in Sdc.CustPOs
                                        where c.Id == customerPOId
                                        select c).SingleOrDefault();
            CustomerPOEntity customerPoEntity = TransformCustPOToCustomerPOEntity(customerPo);
            return customerPoEntity;
        }

        public static CustPO TransformCustomerEntityToCustPO(CustomerPOEntity customerPoEntity)
        {
            CustPO custPo = new CustPO();
            custPo.Address = customerPoEntity.Address;
            custPo.ApprovedBy = customerPoEntity.ApprovedBy;
            custPo.Attention = customerPoEntity.Attention;
            custPo.CreatedBy = customerPoEntity.CreatedBy;
            custPo.CustId = customerPoEntity.CustId;
            custPo.Type = customerPoEntity.Type;
            custPo.Date = customerPoEntity.Date;
            custPo.Fax = customerPoEntity.Fax;
            custPo.No = customerPoEntity.No;
            custPo.Phone = customerPoEntity.Phone;
            custPo.Reference = customerPoEntity.Reference;
            custPo.Remark = customerPoEntity.Remark;
            custPo.Status = customerPoEntity.Status;
            custPo.ZipCode = customerPoEntity.ZipCode;
            custPo.TotalQty = customerPoEntity.TotalQty;
            return custPo;
        }

        public static CustomerPOEntity TransformCustPOToCustomerPOEntity(CustPO custPo)
        {
            CustomerPOEntity customerPoEntity=new CustomerPOEntity();
            customerPoEntity.Address = custPo.Address;
            customerPoEntity.ApprovedBy = custPo.ApprovedBy;
            customerPoEntity.Attention = custPo.Attention;
            customerPoEntity.CreatedBy = custPo.CreatedBy;
            customerPoEntity.CustId = custPo.CustId;
            customerPoEntity.Type = custPo.Type;
            customerPoEntity.Date = custPo.Date;
            customerPoEntity.Fax = custPo.Fax;
            customerPoEntity.No = custPo.No;
            customerPoEntity.Phone = custPo.Phone;
            customerPoEntity.Reference = custPo.Reference;
            customerPoEntity.Remark = custPo.Remark;
            customerPoEntity.Status = custPo.Status;
            customerPoEntity.ZipCode = custPo.ZipCode;
            customerPoEntity.Id = custPo.Id;
            customerPoEntity.CustomerShortName = custPo.Customer.ShortName;
            decimal amount = CountTotalAmount(custPo);
            customerPoEntity.Amount = amount;
            customerPoEntity.CustomerFullName = custPo.Customer.Name;
            customerPoEntity.TotalQty = custPo.TotalQty;

            if(custPo.Type == "purchase" || custPo.TotalQty==0)
            {
                customerPoEntity.TotalOpenQty = custPo.CustPOItems.Sum(p => p.RequestQty) -custPo.CustPOItems.Sum(p => p.CompleteQty);
            }
            else
            {
                customerPoEntity.TotalOpenQty = custPo.TotalQty - custPo.CustPOItems.Sum(p => p.CompleteQty);
            }
            
            return customerPoEntity;
            
        }



        public void updateCustPOStatus(int custPOId, string status)
        {
            CustPO custPo = (from c in Sdc.CustPOs
                             where c.Id == custPOId
                             select c).SingleOrDefault();
            custPo.Status = status;
            Sdc.SubmitChanges();
        }

        
        public void DeleteCustomerPOByCustPOId(int custPOId)
        {
            CustomerPOItemController customerPoItemController = new CustomerPOItemController();
            customerPoItemController.DeleteCustomerPOByCustomerPOId(custPOId);

            CustPO custPo = (from c in Sdc.CustPOs
                             where c.Id == custPOId
                             select c).SingleOrDefault();
            Sdc.CustPOs.DeleteOnSubmit(custPo);
            Sdc.SubmitChanges();
        }

        public void UpateCustomerPO(CustomerPOEntity CustomerPoEntity)
        {
            CustPO custPo = (from c in Sdc.CustPOs
                             where c.Id == CustomerPoEntity.Id
                             select c).SingleOrDefault();
            custPo.No = CustomerPoEntity.No;
            custPo.CustId = CustomerPoEntity.CustId;
            custPo.Phone = CustomerPoEntity.Phone;
            custPo.Reference = CustomerPoEntity.Reference;
            custPo.Remark = CustomerPoEntity.Remark;
            custPo.ZipCode = CustomerPoEntity.ZipCode;
            custPo.Address = CustomerPoEntity.Address;
            custPo.ApprovedBy = CustomerPoEntity.ApprovedBy;
            custPo.CreatedBy = CustomerPoEntity.CreatedBy;
            custPo.Attention = CustomerPoEntity.Attention;
            custPo.Fax = CustomerPoEntity.Fax;
            Sdc.SubmitChanges();
        }

        public IList<CustomerPOEntity> FindCustomerPOByNo(string poNo)
        {
            IList<CustPO> customerPos = (from c in Sdc.CustPOs
                                 where c.No.Contains(poNo)
                                 select c).ToList();
            IList<CustomerPOEntity> customerPoEntities = new List<CustomerPOEntity>();
            foreach (var c in customerPos)
            {
                CustomerPOEntity customerPoEntity = TransformCustPOToCustomerPOEntity(c);
                customerPoEntities.Add(customerPoEntity);
            }
            return customerPoEntities;
        }

        public IList<CustomerPOEntity> FindCustomerPOByDate(DateTime fromDate,DateTime toDate)
        {
            IList<CustPO> customerPos = (from c in Sdc.CustPOs
                                         where c.Date >= fromDate && c.Date <= toDate
                                         orderby c.Date descending, c.Id descending 
                                         select c).ToList();
            IList<CustomerPOEntity> customerPoEntities = new List<CustomerPOEntity>();
            foreach (var c in customerPos)
            {
                CustomerPOEntity customerPoEntity = TransformCustPOToCustomerPOEntity(c);
                customerPoEntities.Add(customerPoEntity);
            }
            return customerPoEntities;
        }

        public IList<CustomerPOEntity> FindAllCustomerPO()
        {
            IList<CustPO> custPos = (from c in Sdc.CustPOs
                                     orderby c.Date descending , c.Id descending
                                     select c).ToList();
            IList<CustomerPOEntity> customerPoEntities=new List<CustomerPOEntity>();
            foreach (CustPO custPo in custPos)
            {
                CustomerPOEntity customerPoEntity = TransformCustPOToCustomerPOEntity(custPo);
                customerPoEntities.Add(customerPoEntity);
            }
            return customerPoEntities;
        }

        public bool CheckCustomerPONoAvailability(string newPONo,ref string msg)
        {
            bool result = true;
            var count = (from c in Sdc.CustPOs
                                     where c.No.ToUpper()==newPONo.ToUpper()
                                     select c).Count();
            if(count>0)
            {
                msg = "PO NO. already existed!";
                return false;
                
            }
            else
            {
                return true;
            }
            
        }
    }
}
